import { bingTranslate } from '../utils/common'

export const validate = (error: any) => {
  const err = { msg: '' }
  const { message, field } = error.errors[0]
  switch (message) {
    case 'required':
      err.msg = field + ' 必填'
      break
    case 'should not be empty':
      err.msg = field + ' 不能为空'
      break
    default:
      err.msg = field + ' ' + message
  }
  if (!err.msg) {
    let strArray: string[] = []
    switch (true) {
      case message.includes('length should smaller than'):
        strArray = message.split(' ')
        err.msg = field + ' 长度应小于等于 ' + strArray[strArray.length - 1]
        break
      case message.includes('length should bigger than'):
        strArray = message.split(' ')
        err.msg = field + ' 长度应大于等于 ' + strArray[strArray.length - 1]
        break
    }
  }
  return err.msg ? err : error
}

export const joiErrHandle = async (error: any) => {
  const { details } = error
  console.log(details)
  let text = details.map((item: any) => item.message)
  text = text.join(',').replaceAll('"', '')
  const msg = await bingTranslate({ text })
  return msg
}
